package com.dormitory.controller;

import com.dormitory.po.Admin;
import com.dormitory.po.Building;
import com.dormitory.po.PageInfo;
import com.dormitory.service.AdminService;
import com.dormitory.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BuildingController {
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private AdminService adminService;

    @RequestMapping("/findBuilding")
    public String findBuilding(String d_dormbuilding, String a_name,
                               Integer pageIndex, Integer pageSize, Model model) {
        PageInfo<Building> bi = buildingService.findPageInfo(d_dormbuilding, a_name, pageIndex, pageSize);
        model.addAttribute("bi", bi);
        return "building_list";
    }

    @RequestMapping("/findBuildingById")
    public String findBuildingById(Integer d_id, HttpSession session, Model model) {
        List<Admin> adminList = adminService.getAll();
        model.addAttribute("adminList", adminList);
        Building b = buildingService.findBuildingById(d_id);
        session.setAttribute("b", b);
        return "building_edit";
    }

    @RequestMapping(value = "/exportbuildinglist", method = RequestMethod.POST)
    @ResponseBody
    public List<Building> exportClass() {
        List<Building> buildingList = buildingService.getAll();
        return buildingList;
    }

    @RequestMapping(value = "/addBuilding", method = RequestMethod.POST)
    @ResponseBody
    public String addBuilding(@RequestBody Building building) {
        buildingService.addBuilding(building);
        return "building_list";
    }

    @RequestMapping(value = "/updateBuilding", method = RequestMethod.POST)
    public String updateBuilding(Building building) {
        buildingService.updateBuilding(building);
        return "redirect:/findBuilding";
    }

    @RequestMapping("/deleteBuilding")
    @ResponseBody
    public String deleteBuilding(Integer d_id) {
        buildingService.deleteBuilding(d_id);
        return "building_list";
    }

}
